package com.gec.yhzh.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gec.yhzh.entity.Department;
import com.gec.yhzh.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 科室 前端控制器
 * </p>
 *
 * @author jerry
 * @since 2025-03-10
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    IDepartmentService iDepartmentService;
    //JSON解析工具
    ObjectMapper objectMapper = new ObjectMapper();
    @GetMapping("/query")
    public String  queryDepartment() throws JsonProcessingException {
        Map<String, Object> result = new HashMap<>();
        List<Department> DepartmentList = iDepartmentService.list();
        if (DepartmentList == null ||DepartmentList.isEmpty()) {
            result.put("flag",false );
            return objectMapper.writeValueAsString(result);
        }
        result.put("flag",true );
        result.put("list",DepartmentList );
        return objectMapper.writeValueAsString(result);
    }
    @GetMapping("/selectByDeptId")
    public  String selectByDeptId(Integer deptId) throws JsonProcessingException {
        Map<String, Object> result = new HashMap<>();
        Department department = iDepartmentService.getById(deptId);
        if (department == null) {
            result.put("flag",false );
            return objectMapper.writeValueAsString(result);
        }
        result.put("flag",true );
        result.put("department",department );
        return objectMapper.writeValueAsString(result);
    }
}
